﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using System.IO;
using System;
using XXX.Bo;
using XXX.Common;
using XXX.Models;
using XXX.Models.AS;
using LH.Model.PU;
using System.Collections.Generic;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.Logging;
using NETCore.MailKit.Core;
using fuzhu;
using Yonyou.U8.BO;
using LH.Model.SA;
using Newtonsoft.Json;
using System.Runtime.ConstrainedExecution;
using XXX.Models.WDFT;
using LH.Model.ST;
using static System.Runtime.CompilerServices.RuntimeHelpers;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using static XXX.Models.MO.API_Morder;
using XXX.Bo.ViewModel;
using System.Threading.Tasks;
using XXX.api.Filter;
using Microsoft.AspNetCore.Routing.Matching;
using ZDEntity;

namespace XXX.api.Controllers.WDFT
{
    /// <summary>
    /// 生产订单
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    //[Authorize]
    [TypeFilter(typeof(ApiLoggingFilterFactory))]
    public class WanJi_SCJHController : BaseController
    {
        public static readonly object _lockObj = new object();
        private readonly ILogger<WanJi_SCJHController> _logger;
        private readonly IEmailService _EmailService;


        public WanJi_SCJHController(ILogger<WanJi_SCJHController> logger, IEmailService emailService)
        {
            _logger = logger;
            _EmailService = emailService;
        }

        [HttpPost]
        public RetModel2 Post(JObject obj)
        {
            //一次只执行一个线程
            lock (_lockObj)
            {
                string requestBody = DbHelper.GetDbString(obj);
                if (requestBody == null)
                {
                    return DBService.ReErrObject("入参有误，没有数据");
                }
                #region 接口调用间隔
                string strKey = ControllerContext.RouteData.Values["controller"].ToString() + Md5Encrypt.MD5(requestBody);

                if (CacheHelpCore.GetCacheValue(strKey) == null)
                {
                    CacheHelpCore.SetChacheValue(strKey, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), CanShu.chkRepeat);
                }
                else
                {
                    throw new Exception("接口调用间隔过短");
                }
                #endregion 接口调用间隔
                try
                {
                    string cErr = "";
                    zdy_wanji_jihua_ViewList model = JsonConvert.DeserializeObject<zdy_wanji_jihua_ViewList>(requestBody);
                    if (obj == null || model.vouList.Count==0)
                    {
                        cErr = "json结构异常!空值";
                        return null;
                    }
                    //string cBatch  = model.cMoCode;
                    RetModel2 ret;
                    if (model != null)
                    {  
                        WanJiSCJHVM wanJiSCJHVM =new WanJiSCJHVM();
                        wanJiSCJHVM.conStr = CanShu.conStr;
                        wanJiSCJHVM.lstscjh = model;
                        ret = wanJiSCJHVM.Add();
                      
                        
                        return ret;

                    }
                    else
                    {
                        return DBService.ReErrObject("空值");
                    } 
                }
                catch (Exception ex)
                {
                    //_EmailService.Send(CanShu.mail.ToAddress, CanShu.cOrg + CanShu.cZT + "生产订单新增,程序错误,", "报文:\r\n" + requestBody + "\r\n" + "结果,\r\n" + ex.Message);
                    return DBService.ReErrObject(ex.Message);
                }

            } 
        }
         

    }
}
